package com.example.login;

import jakarta.servlet.http.HttpSession;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Description:
 * Date: 2024-07-22
 * Time: 19:24
 */
@RestController
@RequestMapping("user")
public class LoginController {

    @RequestMapping("login")
    public boolean login(String username, String password, HttpSession session) {
        if(!StringUtils.hasLength(username) || !StringUtils.hasLength(password)) return false;
        if(!"zhangsan".equals(username) || !"123456".equals(password)) return false;
        session.setAttribute("username",username); //密码验证成功的话，就把用户名存储在 Session 中
        return true;
    }

    @RequestMapping("getLoginUser")
    public String getLoginUser(HttpSession session) {
        String username = (String) session.getAttribute("username");
        if(StringUtils.hasLength(username)) return username; //若用户已登录，则返回用户名
        return "";
    }
}
